package com.ds.infrastructure.audit.admin.util;

import com.ds.infrastructure.audit.admin.entry.UserInfo;
import com.ds.infrastructure.audit.common.constant.CommonConstant;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

public class RequestUtils {

    static private final String USER_INFO = "__user_info__";

    static public void setUserInfo(ServletRequest request, UserInfo userInfo) {
        request.setAttribute(USER_INFO, userInfo);
    }

    static public UserInfo getUserInfo(ServletRequest request) {
        return (UserInfo) request.getAttribute(USER_INFO);
    }

    /**
     * 从request中获取Authorization
     *
     * @param request
     * @return
     */
    public static String getAuthorization(HttpServletRequest request) {
        String sid = request.getParameter(CommonConstant.HTTP_HEADER_AUTHORIZATION);
        if (StringUtils.isBlank(sid)) {
            sid = request.getHeader(CommonConstant.HTTP_HEADER_AUTHORIZATION);
        }
        return sid;
    }

}
